<script setup lang="ts">
import { onShow } from '@dcloudio/uni-app'
import { getMealTypeAPI } from '@/services/category'
import { getMealListAPI, deleteMealAPI } from '@/services/goods'
import type { CategoryTopItem } from '@/types/category'
import type { MealResult } from '@/types/goods'
import { ref } from 'vue'
import { useAddressStore } from '@/stores'
import {
  selectMealAddressAPI
} from '@/services/address'
import { useMemberStore } from '@/stores'
// 获取会员Store
const memberStore = useMemberStore()
// 获取配送中心Store
const AddressStore = useAddressStore()

// 获取配送中心列表数据
const getSelectMealData = async () => {
  const res = await selectMealAddressAPI()
  AddressStore.changeSelectedAddress(res.result)
}

// 获取餐品分类数据
const mealTypeList = ref<CategoryTopItem[]>([])
const activeIndex = ref(0)
const getMealType = async () => {
  const res = await getMealTypeAPI()
  mealTypeList.value = res.result
}
//选择套餐类型
const currentMealType = ref('早餐')
const changeTime = (index: number, type: string) => {
  activeIndex.value = index
  currentMealType.value = type
  getMealList()
}
// 获取套餐列表
const mealList = ref<MealResult[]>([])
const getMealList = async () => {
  const res = await getMealListAPI(currentMealType.value, memberStore.profile!.addressid)
  mealList.value = res.result
}


// 初始化调用(页面显示)
onShow(() => {
  getMealType()
  getMealList()
  getSelectMealData()
})
// 删除出差中心
const onDeleteMeal = (id: string) => {
  // 二次确认
  uni.showModal({
    content: '删除出餐中心?',
    success: async (res) => {
      if (res.confirm) {
        // 根据id删除收货地址
        await deleteMealAPI(id)
        // 重新获取收货地址列表
        getMealList()
      }
    },
  })
}

</script>

<template>
  <view class="viewport">
    <!-- 上侧：一级分类 -->
    <view class="categoriestap">
      <view v-for="(item, index) in mealTypeList" @tap="changeTime(index, item.type)" :key="item.id"
        :class="[index === activeIndex ? 'active' : 'item']">
        <text class="name">
          {{ item.type }}
        </text>
      </view>
    </view>
    <!-- 二级分类 -->
    <scroll-view enable-back-to-top class="scroll-view" scroll-y>
      <view v-if="mealList.length" class="address">
        <uni-swipe-action class="address-list">
          <!-- 收货地址项 -->
          <uni-swipe-action-item class="cart-swipe" v-for="goods in mealList" :key="goods.id">
            <view class="goods">
              <image mode="aspectFill" class="picture" :src="goods.picture"></image>
              <view class="meta">
                <view class="name ellipsis">{{ goods.name }}</view>
                <view class="attrsText ellipsis">{{ goods.taste }}</view>
                <view class="attrsText ellipsis">{{ goods.mealdesc }}</view>
                <view class="price">{{ goods.price }}</view>
              </view>
              <navigator class="edit" hover-class="none" :url="`/pagesMember/meal-form/meal-form?id=${goods.id}`"
                @tap.stop="() => { }">
                修改
              </navigator>
            </view>
            <!-- 右侧插槽 -->
            <template #right>
              <button @tap="onDeleteMeal(goods.id!)" class="delete-button">删除</button>
            </template>
          </uni-swipe-action-item>
        </uni-swipe-action>
      </view>
      <view v-else class="blank">暂无套餐</view>
    </scroll-view>
    <!-- 添加按钮 -->
    <view class="add-btn">
      <navigator hover-class="none" url="/pagesMember/meal-form/meal-form">
        新建套餐
      </navigator>
    </view>
  </view>
</template>

<style lang="scss">
page {
  height: 100%;
  overflow: hidden;
}

/* 删除按钮 */
.delete-button {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 50px;
  height: 100%;
  font-size: 28rpx;
  color: #fff;
  border-radius: 0;
  padding: 0;
  background-color: #cf4444;
}

.viewport {
  display: flex;
  flex-direction: column;
  height: 100%;
  background-color: #f4f4f4;

  .scroll-view {
    padding-top: 20rpx;
  }
}

.address {
  padding: 0 20rpx;
  margin: 0 20rpx;
  border-radius: 10rpx;
  background-color: #fff;

  .item-content {
    line-height: 1;
    padding: 40rpx 10rpx 38rpx;
    border-bottom: 1rpx solid #ddd;
    position: relative;

    .edit {
      position: absolute;
      top: 36rpx;
      right: 30rpx;
      padding: 2rpx 0 2rpx 80rpx;
      border-left: 10rpx solid #666;
      font-size: 26rpx;
      color: #666;
      line-height: 1;
    }
  }

  .item:last-child .item-content {
    border: none;
  }

  .user {
    font-size: 28rpx;
    margin-bottom: 20rpx;
    color: #333;

    .contact {
      color: #666;
    }

    .badge {
      display: inline-block;
      padding: 4rpx 10rpx 2rpx 14rpx;
      margin: 2rpx 0 0 10rpx;
      font-size: 26rpx;
      color: #27ba9b;
      border-radius: 6rpx;
      border: 1rpx solid #27ba9b;
    }
  }

  .locate {
    line-height: 1.6;
    font-size: 26rpx;
    color: #333;
  }
}

.blank {
  margin-top: 300rpx;
  text-align: center;
  font-size: 32rpx;
  color: #888;
}

.add-btn {
  height: 80rpx;
  text-align: center;
  line-height: 80rpx;
  margin: 30rpx 20rpx;
  color: #fff;
  border-radius: 80rpx;
  font-size: 30rpx;
  background-color: #27ba9b;
}


.goods {
  display: flex;
  position: relative;

  .navigator {
    display: flex;
  }

  .checkbox {
    position: absolute;
    top: 0;
    left: 0;

    display: flex;
    align-items: center;
    justify-content: center;
    width: 80rpx;
    height: 100%;

    &::before {
      content: '\e6cd';
      font-family: 'erabbit' !important;
      font-size: 40rpx;
      color: #444;
    }

    &.checked::before {
      content: '\e6cc';
      color: #27ba9b;
    }
  }

  .picture {
    width: 170rpx;
    height: 170rpx;
  }

  .meta {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    margin-left: 20rpx;
  }

  .name {
    height: 72rpx;
    font-size: 26rpx;
    color: #444;
  }

  .attrsText {
    line-height: 1.8;
    padding: 0 15rpx;
    font-size: 24rpx;
    align-self: flex-start;
    border-radius: 4rpx;
    color: #888;
    background-color: #f7f7f8;
  }

  .price {
    line-height: 1;
    font-size: 26rpx;
    color: #444;
    margin-bottom: 2rpx;
    color: #cf4444;

    &::before {
      content: '￥';
      font-size: 80%;
    }
  }

  // 商品数量
  .count {
    position: absolute;
    bottom: 20rpx;
    right: 5rpx;

    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 220rpx;
    height: 48rpx;

    .text {
      height: 100%;
      padding: 0 20rpx;
      font-size: 32rpx;
      color: #444;
    }

    .input {
      height: 100%;
      text-align: center;
      border-radius: 4rpx;
      font-size: 24rpx;
      color: #444;
      background-color: #f6f6f6;
    }
  }
}

.cart-swipe {
  margin: 20rpx 20rpx 0rpx 20rpx;
  border-radius: 10rpx;
  background-color: #fff;
}

.categoriestap {
  display: flex;
  flex-direction: row;
  margin-top: 20rpx;
  margin-bottom: 10rpx;
}

.item {
  font-size: 26rpx;
  margin-left: 15rpx;
  margin-right: 15rpx;
  padding-left: 10rpx;
  padding-right: 10rpx;
  color: #100202;
  border-radius: 10%;
  background-color: #e5e2e2;
}

.active {
  font-size: 26rpx;
  margin-left: 15rpx;
  margin-right: 15rpx;
  padding-left: 10rpx;
  padding-right: 10rpx;
  color: white;
  border-radius: 10%;
  background-color: #27ba9b;
}
</style>
